// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Dynamic/EIGRParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/IEIGRService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (EIGR);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/EIGR/EIGR.md>
*/

// 以下需要修改
enum EIGRIndex : std::uint8_t
{
    EIGRIndex_SID    = 2,
    EIGRIndex_METHOD = 3,
    EIGRIndex_F1     = 4,
    EIGRIndex_F2     = 5,
    EIGRIndex_NE     = 6,
    EIGRIndex_ND     = 7,
    EIGRIndex_NORM   = 12,
    EIGRIndex_G      = 13,
    EIGRIndex_C      = 14

};
SG::DataStructure::Common::Status BDF::BULKDATA::ParseEIGR (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::EIGRData tempEIGR;
    // TODO(bug 001) m_dominId的正确填写
    tempEIGR.m_domainId = 1;  // 暂时将m_domainId设为1，待修复

    int curIDKey = 1;         // 当前字符串是第几项

    // 遍历分割后的每个词条
    for (std::string& curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        // 根据当前词条的位置进行相应的解析
        switch (curIDKey)
        {
        case EIGRIndex_SID:
            tempEIGR.m_sId = convertToI64 (curStrKey);
            break;
        case EIGRIndex_METHOD:
            std::strncpy (
                tempEIGR.m_METHOD, boost::algorithm::trim_copy (curStrKey).c_str (), boost::algorithm::trim_copy (curStrKey).size ());
            break;
        case EIGRIndex_F1:
            tempEIGR.m_F1 = convertToReal (curStrKey);
            break;
        case EIGRIndex_F2:
            tempEIGR.m_F2 = convertToReal (curStrKey);
            break;
        case EIGRIndex_NE:
            tempEIGR.m_NE = convertToI64 (curStrKey);
            break;
        case EIGRIndex_ND:
            tempEIGR.m_ND = convertToI64 (curStrKey);
            break;
        case EIGRIndex_NORM:
            std::strncpy (tempEIGR.m_NORM, curStrKey.c_str (), 4);
            break;
        case EIGRIndex_G:
            tempEIGR.m_G = convertToI64 (curStrKey);
            break;
        case EIGRIndex_C:
            tempEIGR.m_C = convertToI64 (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;  // 处理完当前词条后，索引加1
    }

    return dbServiceFactory->get<IEIGRService> ()->Add (tempEIGR);
}